/*
 * $Id: QETokenizer.m 248 2008-09-08 15:31:38Z jon $
 * Sleeper
 *
 * Created by Jon Gordon on 7/28/08
 * Copyright Jon Gordon.  All rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */


#import "QETokenizer.h"

// may be different in an application other than Sleeper, which uses this
// to break up a string that *should* contain six tokens
static const int EXPECTED_NUMBER_OF_TOKENS = 6;

@interface QETokenizer (Private)
- (void)setDelimiterSet:(NSCharacterSet *)newDelimiterSet;
- (NSCharacterSet *)delimiterSet;
@end

//
// Splits an input string into an array of tokens
//
// Author: $Author: jon@bway.net $
// Revision: $Revision: 2 $
//
@implementation QETokenizer : NSObject
+ (void)initialize {
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    static BOOL initialized = NO;
    if (NO == initialized) {
        initialized = YES;
        NSLog(@"  initializing class QETokenizer");
    } // if
} // initialize


- (id)initWithDelimitersFromCharacterSet:(NSCharacterSet *)newDelimiterSet {
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    self = [super init];
    if (nil != self) {
        [self setDelimiterSet:newDelimiterSet];
    } // if
    return self;
} // initWithDelimitersFromCharacterSet:

- (id)initWithDelimitersFromString:(NSString *)delimiters
{
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [self initWithDelimitersFromCharacterSet:[NSCharacterSet characterSetWithCharactersInString:delimiters]];
} // initWithDelimitersFromString:

- (id)initWithDelimiter:(unichar)delimiter
{
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [self initWithDelimitersFromString:[NSString stringWithCharacters:(&delimiter) length:1]];
} // initWithDelimiter:

- (id)init
{
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [self initWithDelimitersFromCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
} // init


- (void)dealloc
{
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    [delimiterSet release];
    [super dealloc];
} // dealloc


- (void)setDelimiterSet:(NSCharacterSet *)newDelimiterSet {
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    if (newDelimiterSet != delimiterSet) {
        [delimiterSet autorelease];
        delimiterSet = [newDelimiterSet copy];
    } // if
} // setDelimiterSet:

- (NSCharacterSet *)delimiterSet
{
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [[delimiterSet retain] autorelease];
} // delimiterSet


- (NSArray *)tokenize:(NSString *)src {
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    NSMutableArray  *tokenList  = [NSMutableArray arrayWithCapacity:EXPECTED_NUMBER_OF_TOKENS];
    NSMutableString *token      = [NSMutableString string];
    NSCharacterSet  *delimiters = [self delimiterSet];
    
    unichar c;
    BOOL inToken = NO;
    unsigned int i = 0;
    unsigned int l = [src length];
    
    while (i < l) {
        c = [src characterAtIndex:i];
        ++i;
        if (inToken) {
            if ([delimiters characterIsMember:c]) {
                inToken = NO;
                [tokenList addObject:[[[NSString alloc] initWithString:token] autorelease]];
            } else {
                [token appendFormat:@"%C", c];
            } // else
        } else {
            if (![delimiters characterIsMember:c]) {
                inToken = YES;
                [token setString:@""];
                [token appendFormat:@"%C", c];
            } // if
        } // else
    } // while
    if (inToken) {
        [tokenList addObject:[NSString stringWithString:token]];
    } // if
    
    NSArray *result = [NSArray arrayWithArray:tokenList];    
    return result;
} // tokenize
@end
